package com.ricchieri.dao;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.ricchieri.dao.interf.IOperationTotalAmountDao;
import com.ricchieri.filter.OperationTotalAmountFilter;
import com.ricchieri.model.OperationTotalAmount;

public class OperationTotalAmountDao extends GenericDao implements IOperationTotalAmountDao {

	private static final long serialVersionUID = -4509901699946009365L;

	private Criteria criteriaAllOperationTotalAmount(OperationTotalAmountFilter filter) {
		Criteria criteria = super.criteriaAllBasic(filter, OperationTotalAmount.class);
		
		if(filter.getOperation() > 0) {
			criteria.createAlias(getTableName().concat(".operation"), "operation");
			criteria.add(Restrictions.eq("operation.id", filter.getOperation()));
		}
		
		return criteria;
	}	
	
	@Override
	protected String getTableName() {
		return "operationTotalAmount";
	}

	@Override
	public OperationTotalAmount getOperationTotalAmountById(OperationTotalAmountFilter filter) {
		Criteria criteria = criteriaAllOperationTotalAmount(filter);
		return (OperationTotalAmount) criteria.list().get(0);
	}

}
